var loginer = App.get("loginer");
App.set("$Event.show_add_course", function () {
	App.model.toggle("$Cache_new_course_flag")
});

function getCourseList() {
	$.ajax({
		url: "/api/complex/MyCourse",
		type: "get",
		data: {
			tid: loginer.tid
		},
		success: function (r) {
			App.set("$Cache_course_list", r);
			console.log(r);
		},
		error: function (e) {
			console.log(e);
		}
	})
}
getCourseList();

// 添加新的课程
App.set("$Event.add_course", function () {
	var course_name = App.get("$Cache_new_course_name");
	if (!course_name) {
		alert("请填写课程名称！！！");
	} else if (!App.get("$Cache_new_course_type")) {
		alert("请选择课程类型！！！");
	} else {
		$.ajax({
			url: "/api/complex/MyCourse",
			type: "post",
			data: {
				coursetype: App.get("$Cache_new_course_type"),
				coursename: course_name,
				tid: loginer.tid,
				tname: loginer.nickname,
				sid: ""
			},
			success: function (r) {

			},
			error: function (e) { }
		})
	};
});
// 删除课程及其内容(视频和日历事件)
// 删除课程及其内容(视频和日历事件)
App.set("$Event.remove_course", function (e, vi) {
	App.set("$Cache_show_course_detail", false);
	myConfirm("确定要删除此课程及其内容吗？？？", function () {
		console.log(vi.get('cid'));
		var data = {
			cid: vi.get("cid"),
			coursetype: vi.get("coursetype")
		};
		$.ajax({
			url: "/api/complex/MyCourse",
			data: data,
			type: "delete",
			success: function (result) {
				console.log(result);
				alert("删除成功");
				vi.remove();
			},
			error: function (e) {
				console.log(e);
			}
		});
		console.log(data);
	});
});
// 获取选定课程下的视频或日历事件
App.set("$Event.get_course_detail", function (e, vi) {
	App.set("$Cache_now_show_course_name", vi.get("coursename"));
	App.set("$Cache_now_show_course_cid", vi.get("cid"));
	App.set("$Cache_now_show_course_type", vi.get("coursetype"));
	$.ajax({
		url: "/api/complex/MyCourseDetail",
		type: "get",
		data: {
			coursetype: vi.get("coursetype"),
			cid: vi.get("cid")
		},
		success: function (r) {
			App.set("$Cache_now_show_course_detail", r);
			console.log(r);
			if (App.get("$Cache_now_show_course_type") == 1) {
				$('#course_date').datetimepicker({
					format: 'yyyy-MM-dd',
					language: 'en',
					pickDate: true,
					// pickTime: true,
					// hourStep: 1,
					// minuteStep: 15,
					// secondStep: 30,
					inputMask: true
				});
			};
		},
		error: function (e) {
			console.log(e);
		}
	})
});
// 添加当前课程内容
App.set("$Event.add_course_detail", function () {
	if (!($("[name='end']").val() && App.get("$Cache_new_calendar_course_text"))) {
		alert("请先确定课程日期和内容");
		return;
	} else {
		var _id = (new Date()).getTime();
		var _cid = App.get("$Cache_now_show_course_cid");
		var _title = loginer.nickname + "，" + App.get("$Cache_now_show_course_name") + "，" + App.get("$Cache_new_calendar_course_text");
		var _start = $("[name='end']").val();
		var data = {
			coursetype: App.get("$Cache_now_show_course_type"),
			calendar: {
				id: _id,
				cid: _cid,
				title: _title,
				tid: loginer.tid,
				start: _start,
				end: " " + '24:00:00',
				color: 0
			},
			coursecalendar: {
				id: _id,
				cid: _cid,
				title: _title,
				tid: loginer.tid,
				start: _start,
				color: "0",
				cname: App.get("$Cache_now_show_course_name"),
				tname: loginer.nickname
			}
		};
		console.log(data);
		$.ajax({
			url: "/api/complex/MyCourseDetail",
			type: "post",
			data: data,
			success: function (r) {
				var $Cache_now_show_course_detail = App.get("$Cache_now_show_course_detail");
				$Cache_now_show_course_detail.unshift(r);
				App.set("$Cache_now_show_course_detail", $Cache_now_show_course_detail);
				console.log(r);
			},
			error: function (e) {
				console.log(e);
			}
		});
	};
});
// 删除单独日历事件内容
App.set("$Event.delete_calendar_one", function (e, vi) {
	console.log(vi.get("id"));
	myConfirm("确定要删除此事件吗？？？", function () {
		$.ajax({
			url: "/api/complex/MyCourseDetail",
			type: 'delete',
			data: {
				coursetype: App.get("$Cache_now_show_course_type"),
				id: vi.get('id')
			},
			success: function (r) {
				console.log(r);
				vi.remove();
			},
			error: function (e) {
				console.log(e);
			}
		})
	})
});
// 删除单独视频内容
App.set("$Event.delete_video_one", function (e, vi) {
	myConfirm("确定要删除此视频吗？？？", function () {
		$.ajax({
			url: "/api/complex/MyCourseDetail",
			type: 'delete',
			data: {
				id: vi.get('id')
			},
			success: function (r) {
				console.log(r);
				vi.remove();
			},
			error: function (e) {
				console.log(e);
			}
		});
	});
});
// 上传视频
App.set("$Event.open_video_up_file", function () {
	$("#course_video").click();
});
App.set("$Event.new_course_video_up", function () {
	var _dir = fileuploadfull(this);
	App.set("$Cache_new_video_url", _dir.savedir);
	App.set("$Cache_new_video_path", _dir.path);
	App.set("$Cache_new_video_type", 1);
	App.set("$Cache_url_self", true);
});

// 添加新的课程视频
App.set("$Event.add_course_new_video", function () {
	var _url = App.get("$Cache_new_video_url") || App.get("$Cache_new_video_path");
	var _name = App.get("$Cache_new_video_name");
	console.log(_url, _name);

	if (!(_url && _name)) {
		alert("请填写视频地址和名称");
	} else {
		$.ajax({
			url: "/api/complex/MyCourseDetail",
			type: "post",
			asycn: false,
			data: {
				name: _name,
				url: _url,
				date: (new Date()).toLocaleString(),
				cid: App.get("$Cache_now_show_course_cid"),
				tid: loginer.tid,
				chat: "[]",
				sid: "[]",
				tname: loginer.nickname,
				type: App.get("$Cache_new_video_type")
			},
			success: function (r) {
				console.log(r);
				var $Cache_now_show_course_detail = App.get("$Cache_now_show_course_detail");
				$Cache_now_show_course_detail.unshift(r);
				App.set("$Cache_now_show_course_detail", $Cache_now_show_course_detail);
				App.set("$Cache_new_video_url", "");
				App.set("$Cache_new_video_path", "");
				App.set("$Cache_new_video_type", '');
				App.set("$Cache_url_self", false);
			},
			error: function (e) {
				console.log(e);
			}
		})
	};
})
// 日期插件
$('#course_date').datetimepicker({
	format: 'yyyy-MM-dd',
	language: 'en',
	pickDate: true,
	// pickTime: true,
	// hourStep: 1,
	// minuteStep: 15,
	// secondStep: 30,
	inputMask: true
});